//
//  HMWelcomeViewController.swift
//  WeiBoXR
//
//  Created by acer on 16/9/23.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
import SDWebImage
class HMWelcomeViewController: UIViewController {

    
    private lazy var backImageV = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var userImageV:UIImageView = { () -> UIImageView in
        var img = UIImageView(image: UIImage(named: "avatar_default_big"))
       
        if let imageUrl: String = HMUserAccountViewModel.sharedUserAccountViewModel.userAccountModel?.avatar_large {
            //  表示url不为nil
            img.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            
        }

        img.layer.cornerRadius = 85 / 2
        img.layer.masksToBounds = true
        return img
        
    }()
    
    
    private lazy var messageLabel = { () -> UILabel in 
        let label = UILabel()
        if let name = HMUserAccountViewModel.sharedUserAccountViewModel.userAccountModel?.name {
            
            label.text = "欢迎回来,\(name)"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 15)
        label.numberOfLines = 0
        label.alpha = 0
        return label
    }()
    
    override func loadView() {
        view = backImageV
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    private func setupUI()  {
        
        view.addSubview(userImageV)
        view.addSubview(messageLabel)
        
        userImageV.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageV)
            make.top.equalTo(userImageV.snp_bottom).offset(18)
        }
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        userImageV.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animate(withDuration: 1, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        //完成之后回到首页
                        NotificationCenter.default.post(name: Notification.Name(switchRootViewController), object: nil)
                })
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
